package com.beaubot.audio.play;

import javax.sound.sampled.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.stream.Stream;
import javazoom.jl.player.Player;

public class PlayMp3 {

    /**
     * Java Music 获取音频文件信息
     * @Title: get_info
     * @Description: 获取音频文件信息
     * @param path 音频文件路径
     * @return 音频文件信息
     * @date 2019年10月25日 下午12:28:41
     */
    public static String get_info(String path) {
        File file=new File(path);
        AudioInputStream ais;
        String result = "";
        try {
            ais = AudioSystem.getAudioInputStream(file);
            AudioFormat af = ais.getFormat();
            result = af.toString();
            System.out.println(result);
            System.out.println("音频总帧数："+ais.getFrameLength());
            System.out.println("每秒播放帧数："+af.getSampleRate());
            float len = ais.getFrameLength()/af.getSampleRate();
            System.out.println("音频时长（秒）："+len);
            System.out.println("音频时长："+(int)len/60+"分"+(int)len%60+"秒");
        } catch(UnsupportedAudioFileException e) {
            throw new RuntimeException(e.getMessage());
        } catch(IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public static void out(String line) {
        System.out.println(line);
    }

    public static void aa(File file) throws Exception {
        AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
        AudioFormat audioFormat = audioInputStream.getFormat();// 或者audioFileFormat.getFormat()
        out("---------AudioFileFormat---------");
        out("Type " + audioFileFormat.getType());
        out("byteLength " + audioFileFormat.getByteLength());
        out("frame length " + audioFileFormat.getFrameLength());
        out("format " + audioFileFormat.getFormat());
        out("properties " + audioFileFormat.properties());
        out("--------------AudioInputStream-------");
        out("frameLength " + audioInputStream.getFrameLength());
        out("--------AudioFormat----------");
        out("encoding " + audioFormat.getEncoding());
        out("channels " + audioFormat.getChannels());
        out("sampleRate " + audioFormat.getSampleRate());
        out("frameRate " + audioFormat.getFrameRate());
        out("properties " + audioFormat.properties());
        out("sampleSizeInBits " + audioFormat.getSampleSizeInBits());
        out("frameSize " + audioFormat.getFrameSize());
    }

    public static void main(String[] args) throws Exception{
        Stream.of(AudioSystem.getAudioFileTypes()).forEach(System.out::println);
        String path = "E:\\data\\nv.mp3";
        aa(new File(path));
//        get_info(path);
//        File file = new File(path);
////        FileInputStream stream=new FileInputStream(file);
////        Player player=new Player(stream);
////        player.play();
//
//        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
//        long frameLength = ais.getFrameLength();
//        AudioFormat audioFormat = ais.getFormat();
//        float rate = audioFormat.getSampleRate();
//        int channels = audioFormat.getChannels();
//        int frameSize = audioFormat.getFrameSize();
//        AudioFormat.Encoding encoding = audioFormat.getEncoding();
//        System.out.println("rate: " + rate);
//        System.out.println("channels: " + channels);
//        System.out.println("frameSize: " + frameSize);
//        System.out.println("encoding: " + encoding);
//        System.out.println("frameLength: " + frameLength);
    }
}
